﻿using JinfuyuanProject.App_Start;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.SessionState;

namespace JinfuyuanProject
{
    public class MvcApplication : System.Web.HttpApplication
    {
        private static Timer _timer;
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            AutofacConfig.AutofacRegister();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            var scheduler = StdSchedulerFactory.GetDefaultScheduler().Result;
            scheduler.Start();
            // 调度数据库备份任务
            new AutofacConfig().ScheduleDatabaseBackupJob(scheduler);
            // 调度工资发放任务
            new AutofacConfig().SchedulePaySalaryJob(scheduler);
            //数据库监听依赖注册
            SqlDependency.Start(ConfigurationManager.ConnectionStrings["Jinfuyuan2025"].ConnectionString);
        }
        protected void Application_End(object sender, EventArgs e)
        {
            //当被监测的数据库中的数据发生变化时,SqlDependency会自动触发OnChange事件来通知应用程序,从而达到让系统自动更新数据(或缓存)的目的。
            SqlDependency.Stop(ConfigurationManager.ConnectionStrings["Jinfuyuan2025"].ConnectionString);
        }
        protected void Application_PostAuthorizeRequest()
        {
            // 允许 SignalR 访问 Session
            if (Context.Request.AppRelativeCurrentExecutionFilePath.StartsWith("~/signalr"))
            {
                Context.SetSessionStateBehavior(SessionStateBehavior.Required);
            }
        }
    
}

}
